﻿using FocusClock.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FocusClock
{
    public partial class Main : Form
    {
        private ClockManager clockManager = new ClockManager();
        public Main()
        {
            InitializeComponent();
        }



        protected override void OnLoad(EventArgs e)
        {
            ClockManager_onReset();
            this.Size = panel_stop.Size = panel_running.Size;

            //右下角启动
            InitLocation();

            clockManager.onRunning += ClockManager_onRunning;
            clockManager.onStart += ClockManager_onStart;
            clockManager.onReset += ClockManager_onReset;

            panel_running.MouseDown += Panel_MouseDown;
            panel_running.MouseMove += Panel_MouseMove;

            panel_stop.MouseDown += Panel_MouseDown;
            panel_stop.MouseMove += Panel_MouseMove;

            SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            UpdateStyles();

            //时间参数初始化

            textBox_rest.Text = "5";
            textBox_foucs.Text = "30";
        }

        private void InitLocation()
        {
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height);
        }

        private void ClockManager_onReset()
        {
            panel_running.Visible = false;
            panel_stop.Visible = true;
            panel_stop.Location = new Point(0, 0);
        }

        private void ClockManager_onStart()
        {
            panel_running.Visible = true;
            panel_stop.Visible = false;

            panel_running.Location = new Point(0, 0);
        }

        private void ClockManager_onRunning()
        {
            this.Invoke(() =>
            {
                if (clockManager == null) return;
                var countDown = Math.Abs(clockManager.Countdown);
                var minutes = countDown / 60;
                if (minutes > 99)
                {
                    minutes = 99;
                }
                var seconds = countDown % 60;

                if (minutes > 0)
                {
                    label_countdown_second.Visible = true;
                    label_countdown_minute.Text = $"{(minutes < 10 ? "0" : "")}{minutes}";
                    label_countdown_second.Text = $"{(seconds < 10 ? "0" : "")}{seconds}";

                    label_countdown_second.Location = new Point(label_countdown_minute.Location.X + label_countdown_minute.Size.Width - 10, label_countdown_second.Location.Y);
                    label_countdown_second.BringToFront();
                }
                else
                {
                    label_countdown_second.Visible = false;
                    label_countdown_minute.Text = $"{(seconds < 10 ? "0" : "")}{seconds}";
                }




                RunningStateChange();

                //超时处理
                if (clockManager.IsTimeout)
                {
                    if (clockManager.IsRunning)
                    {
                        button_pauseOrContinue.BackgroundImage = Properties.Resources.play;

                        if (countDown % 2 == 0)
                        {
                            panel_running.BackColor = Color.OrangeRed;
                        }
                        else if (countDown % 2 == 1)
                        {
                            panel_running.BackColor = Color.LightSeaGreen;
                        }
                    }

                    label_countdown_second.ForeColor = Color.White;
                    label_countdown_minute.ForeColor = Color.White;
                    label_status.ForeColor = Color.White;
                }
                else
                {
                    if (panel_running.BackColor != Color.WhiteSmoke)
                    {
                        panel_running.BackColor = Color.WhiteSmoke;
                    }

                    label_countdown_second.ForeColor = Color.FromArgb(64, 64, 64);
                    label_countdown_minute.ForeColor = Color.FromArgb(64, 64, 64);
                    label_status.ForeColor = Color.FromArgb(64, 64, 64);
                }

                //运行状态
                if (clockManager.ActionType == ClockActionType.Focus)
                {
                    label_status.Text = "专注";
                }
                else if (clockManager.ActionType == ClockActionType.Rest)
                {
                    label_status.Text = "休息";
                }
            });
        }

        private void button_pauseOrContinue_Click(object sender, EventArgs e)
        {
            if (clockManager.IsRunning && clockManager.IsTimeout)
            {
                if (clockManager.ActionType == ClockActionType.Focus)
                {
                    clockManager.StopFoucs();
                }
                else if (clockManager.ActionType == ClockActionType.Rest)
                {
                    clockManager.StopRest();
                }
            }
            else
            {
                clockManager.PauseOrContinue();
            }
            RunningStateChange();
        }

        private void RunningStateChange()
        {
            if (clockManager.IsRunning)
            {
                button_pauseOrContinue.BackgroundImage = Properties.Resources.stop;
                button_reset.Visible = false;
            }
            else
            {
                button_pauseOrContinue.BackgroundImage = Properties.Resources.play;
                button_reset.Visible = true;
            }
        }

        private void button_start_Click(object sender, EventArgs e)
        {
            try
            {
                clockManager.RestTime = int.Parse(textBox_rest.Text) * 60;//外面传的是一分钟，但是要按秒计算
                clockManager.FoucsTime = int.Parse(textBox_foucs.Text) * 60;//外面传的是一分钟，但是要按秒计算
            }
            catch
            {
                clockManager.RestTime = 5 * 60;
                clockManager.FoucsTime = 30 * 60;
            }
            textBox_rest.Text = clockManager.RestTime / 60 + "";
            textBox_foucs.Text = clockManager.FoucsTime / 60 + "";

            clockManager.Start();
        }

        private void button_reset_Click(object sender, EventArgs e)
        {
            clockManager.Rest();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            if (clockManager != null)
            {
                clockManager.Dispose();
            }
        }

        Point mPoint;

        private void Panel_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void Panel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);

            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox_foucs_TextChanged(object sender, EventArgs e)
        {
            IsNumber(textBox_foucs);
        }



        private void textBox_rest_TextChanged(object sender, EventArgs e)
        {
            IsNumber(textBox_rest);
        }

        private void IsNumber(TextBox textBox)
        {
            var text = textBox.Text;
            var regext = new Regex(@"^[1-9]\d*$");

            if ((!string.IsNullOrWhiteSpace(text)) && !regext.IsMatch(text))
            {
                textBox.Text = "";
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            InitLocation();
        }
    }
}
